package kr.co.mcshop.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import kr.co.mcshop.review.ReviewBoard;
import kr.co.mcshop.review.ReviewBoardService;
import kr.co.mcshop.reviewComment.ReviewComment;
import kr.co.mcshop.reviewComment.ReviewCommentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping(value="/admin")
@Controller
public class AdminReviewController {
	
	@Autowired
	private ReviewBoardService board;
	
	@Autowired
	private ReviewCommentService comment;
	
	@RequestMapping(value = "/Review", method = RequestMethod.GET)
	public String ReviewList(Model model, @RequestParam Map map) {

		List<ReviewBoard> BOARD = board.getAllBoard(map);

		model.addAttribute("BOARD", BOARD);

		return "admin/review/Review";
	}

	@RequestMapping(value = "/Review_detail", method = RequestMethod.GET)
	public String ReviewDetailOne(Model model, @RequestParam Map map, HttpServletRequest request) {
		board.updateCount(map);
		List<ReviewBoard> BOARD = board.getAllBoard(map);
		List<ReviewComment> COMMENT = comment.getAllComment(map);
		model.addAttribute("BOARD", BOARD.get(0));
		model.addAttribute("COMMENT", COMMENT);
		return "admin/review/Review_detail";

	}
	
	@RequestMapping(value = "/SSearch", method = RequestMethod.POST)
	   public String search(String board_title, Model model, @RequestParam Map map) {
	       ArrayList<ReviewBoard> list = new ArrayList<ReviewBoard>();
	       System.out.println("2222"+map);
	      if (map.get("searchtxt") == null) {
	         return "redirect:/ReviewList";
	      }
	      if(map.get("search").equals("0")){
	         map.put("searchtxt", map.get("searchtxt"));
	         List<ReviewBoard> titleSearchResult = board.searchtitle(map);
	         
	         for(ReviewBoard qnalist : titleSearchResult){
	            list.add(qnalist);
	         }
	      }else if(map.get("search").equals("1")){
	         map.put("searchtxt", map.get("searchtxt"));
	         List<ReviewBoard> userIdSearchResult = board.searchuserid(map);
	         
	         for(ReviewBoard qnalist : userIdSearchResult){
	            list.add(qnalist);
	         }
	      }
	      
	      model.addAttribute("BOARD", list);
	      return "admin/review/Review";
	}
	
	@RequestMapping(value = "deletereview", method = RequestMethod.GET)
	public String deletereview(@RequestParam Map map) {
		board.deletereview(map);
		comment.deletereviewComment(map);
		return "redirect:Review";
	}
	
	
	@RequestMapping(value = "deletereviewcomment", method = RequestMethod.GET)
	public String deletereviewcomment(@RequestParam Map map) {
		comment.deletereviewcomment(map);
		return "redirect:Review";
	}
	
	
}
